﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Boom.Navigation;

namespace Boom.GameObjects
{
    public class ListBoom : DrawableGameComponent
    {
        private Texture2D _TextureBoom;
        private Texture2D _TextureFire;

        protected int _MaxBoom;
        protected int _Lenght;
        protected int _TimeBoom;

        private List<Boom> _List;
        private Vector2 _Position;

        private CheckKeyBoard _CKey;
        private List<int> _PositionBrick;
        private int _Type;

        public int MaxBoom
        {
            get { return _MaxBoom; }
            set
            {
                if (value <= 7)
                {
                    _MaxBoom = value;
                }
            }
        }

        public List<Boom> List
        {
            get { return _List; }
            set { _List = value; }
        }

        public Vector2 Position
        {
            get { return _Position; }
            set { _Position = value; }
        }

        public List<int> PositionBrick
        {
            get { return _PositionBrick; }
            set { _PositionBrick = value; }
        }

        public int Lenght
        {
            get { return _Lenght; }
            set
            {
                if (value <= 6)
                {
                    _Lenght = value;
                }
            }
        }

        public int Type
        {
            get { return _Type; }
            set { _Type = value; }
        }

        public ListBoom(Game Mygame, ref Texture2D Texture, ref Texture2D TextureFire, CheckKeyBoard CKey, List<int> PositionBrick, int Type)
            : base(Mygame)
        {
            _List = new List<Boom>();
            if (CKey != null)
                _CKey = CKey;
            _PositionBrick = PositionBrick;
            _TextureBoom = Texture;
            _TextureFire = TextureFire;
            _Lenght = Config.DefaultLenghtFire;
            _TimeBoom = Config.TimeBoom;
            _MaxBoom = Config.DefaultMaxBoom;
            _Type = Type;
        }

        //Khởi tạo 1 quả boom mới
        public void CheckNewBoom()
        {
            bool rs = true;
            Position = new Vector2((int)(Position.X + 40 / 2) / 40 * 40, (int)(Position.Y + 40 / 2) / 40 * 40);
            Rectangle RectnewBoom = new Rectangle((int)Position.X, (int)Position.Y, 40, 40);
            for (int i = 0; i < List.Count; i++)
            {
                if (RectnewBoom.Intersects(List[i].Bounds))
                    rs = false;
            }
            if (rs)
            {
                bool CheckObs = Type == 3 ? false : true;
                Boom newBoom = new Boom(Game, ref _TextureBoom, ref _TextureFire, Position, _Lenght, _PositionBrick, CheckObs);
                List.Add(newBoom);
            }
            if (_Type != 4)
                CheckExplosion();

        }

        //Kiểm tra nổ dây chuyển của boom
        private void CheckExplosion()
        {
            for (int i = 0; i < List.Count - 1; i++)
            {
                TimeSpan temp1 = List[i].ElapsedBoom;
                TimeSpan temp2 = List[i].ElapsedDisp;

                for (int j = i + 1; j < List.Count; j++)
                {
                    Vector2 comp = (List[i].Position - List[j].Position);
                    bool CheckObs = !Check_Obs(List[i].Position, List[j].Position);
                    if (comp.Length() < (Lenght + 1) * 40 && (comp.X == 0 || comp.Y == 0) && CheckObs)
                    {
                        List[j].ElapsedBoom = temp1;
                        List[j].ElapsedDisp = temp2;
                        Boom temp = List[j];
                        List.RemoveAt(j);
                        List.Insert(i + 1, temp);
                    }
                }

            }
        }

        //Kiểm tra giữa 2 boom có vật cản không
        private bool Check_Obs(Vector2 Pos1, Vector2 Pos2)
        {
            int X1 = (int)(Pos1.X < Pos2.X ? Pos1.X : Pos2.X);
            int X2 = (int)(Pos1.X > Pos2.X ? Pos1.X : Pos2.X);
            int Y1 = (int)(Pos1.Y < Pos2.Y ? Pos1.Y : Pos2.Y);
            int Y2 = (int)(Pos1.Y > Pos2.Y ? Pos1.Y : Pos2.Y);

            for (int i = 0; i < PositionBrick.Count; i++)
            {
                Vector2 temp = new Vector2(PositionBrick[i] % 15 * 40, PositionBrick[i] / 15 * 40);
                if (((temp.Y == Y1 && temp.Y == Y2) && temp.X > X1 && temp.X < X2)
                    || ((temp.X == X1 && temp.X == X2) && temp.Y > Y1 && temp.Y < Y2))
                {
                    return true;
                }

            }
            return false;
        }

        //Khởi tạo các đối tượng lửa
        public override void Initialize()
        {
            List.Clear();
            for (int i = 0; i < List.Count; i++)
            {
                List[i].Initialize();
            }
            base.Initialize();
        }

        //Cập nhật đối tượng boom và lửa
        public override void Update(GameTime gameTime)
        {
            if (_CKey != null)
            {
                if (_CKey.Space && List.Count < MaxBoom)
                    CheckNewBoom();
            }

            for (int i = 0; i < List.Count; i++)
            {
                List[i].Update(gameTime);
                if (!List[i].ListFires.Visible)
                {
                    List.RemoveAt(i);
                }
            }
            base.Update(gameTime);


        }

        //Vẽ các đối tượng lên màn hình
        public override void Draw(GameTime gameTime)
        {
            for (int i = 0; i < List.Count; i++)
            {
                List[i].Draw(gameTime);
            }
            base.Draw(gameTime);
        }
    }
}
